 

#import "NT_FootAddView.h"

//
@interface NT_FootAddView ()<UITextFieldDelegate>
/** view*/
@property (nonatomic, strong) UIView *whiteView ;
/** biaoti*/
@property (nonatomic, strong) UILabel *titleLabel;
/** queren*/
@property (nonatomic, strong) UIButton *sureBtn;
/** quxiao*/
@property (nonatomic, strong) UIButton *cancelBtn;
/** shuru */
@property (nonatomic, strong) UITextField *textFields;
/** xian*/
@property (nonatomic, strong) UIView *lineView;
/** */
@property (nonatomic, strong) NT_CoreDataBase *data;


@end

@implementation NT_FootAddView

- (instancetype)init {
    if ([super init]) {
        self.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        _data = [[NT_CoreDataBase alloc]initWithCoreData:@"NoteCategory" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) { }];
        //
        [self addSubview:self.whiteView];
        [self.whiteView addSubview:self.titleLabel];
        [self.whiteView addSubview:self.textFields];
        [self.whiteView addSubview:self.lineView];
        [self.whiteView addSubview:self.sureBtn];
        [self.whiteView addSubview:self.cancelBtn];
        
    }
    return self;
}


- (UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc]initWithFrame:CGRectMake(1, kMainScreenHeight/2 - kRelativeWidth(100), kMainScreenWidth - 2, kRelativeHeight(150))];
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.layer.cornerRadius = 6;
        _whiteView.layer.masksToBounds = YES;
    }
    return _whiteView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kRelativeWidth(20), kRelativeHeight(20), kRelativeWidth(200), kRelativeHeight(20))];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = FontPFRegular(kRelativeWidth(16));
        _titleLabel.text = @"New Note Classification !";
    }
    return _titleLabel;
}
- (UITextField *)textFields {
    if (!_textFields) {
        _textFields = [[UITextField alloc]initWithFrame:CGRectMake(kRelativeWidth(70), CGRectGetMaxY(self.titleLabel.frame)+kRelativeHeight(20), kMainScreenWidth - kRelativeWidth(140), kRelativeHeight(30))];
        _textFields.textColor = [UIColor lightGrayColor];
        _textFields.placeholder = @"Input a Note Classification";
        _textFields.delegate = self;
    }
    return _textFields;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.textFields.frame), CGRectGetMaxY(self.textFields.frame), CGRectGetWidth(self.textFields.frame), 0.8)];
       _lineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineView;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.lineView.frame)+kRelativeHeight(10), (kMainScreenWidth-2) / 2, kRelativeHeight(30))];
        [_sureBtn setTitle:@"sure" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_sureBtn.titleLabel setFont:FontPFRegular(kRelativeWidth(14))];
        [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake((kMainScreenWidth-2) / 2, CGRectGetMaxY(self.lineView.frame)+kRelativeHeight(10), (kMainScreenWidth-2) / 2, kRelativeHeight(30))];
        [_cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_cancelBtn.titleLabel setFont:FontPFRegular(kRelativeWidth(14))];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (void)showV {
    
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self removeFromSuperview];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (_textFields == textField) {
        return [self validateNumber:string];
    }
    return YES;
}
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
//
- (void)cancelAction {
    [self removeFromSuperview];
}
//
- (void)sureAction {
    //
    if (_textFields.text.length <= 0) {
        return ;
    }
    UIImage *img = [UIImage imageNamed:@"book9"];
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[self random] forKey:@"id"];
     [dic setObject:_textFields.text forKey:@"title"];
     [dic setObject:img forKey:@"picture"];
    //
    __weak typeof(self) weakSelf = self;
    [_data ntInsertNewEntity:dic success:^{
        [self removeFromSuperview];
        if (weakSelf.reloadBlock) {
            weakSelf.reloadBlock();
        }
    } fail:^(NSError * _Nullable error) {
    }];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (NSString *)random
{
    const int count =6;
    NSString * sourceString=@"123456789";
    NSMutableString *re=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[sourceString length];
        NSString * s=[sourceString substringWithRange:NSMakeRange(index, 1)];
        [re appendString:s];
    }
    return re;
}
@end
